package karaokestrong.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import karaokestrong.excepcion.DAOExcepcion;
import karaokestrong.modelo.Cancion;
import karaokestrong.negocio.GestionCancion;

/**
 * Servlet implementation class BuscarProductosServlet
 */
@WebServlet("/BuscarProductosServlet")
public class BuscarProductosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BuscarProductosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			System.out.println("Entre a Buscar Servlet");
			String valorABuscar = request.getParameter("txtSearch")!=null?request.getParameter("txtSearch"):"";
			String tipoFiltro = request.getParameter("cbTipoFiltro")!=null?request.getParameter("cbTipoFiltro"):"";

			GestionCancion negocio = new GestionCancion();
			Collection<Cancion> c = new ArrayList<>();

			switch(tipoFiltro){
			case "1":
				c = negocio.obtenerCancionPorNombre(valorABuscar.trim());
				break;
			case "2":
				c=negocio.obtenerCancionPorArtista(valorABuscar.trim());
				break;
			default:
				break;
			}
			request.setAttribute("txtSearch", valorABuscar);
			request.setAttribute("cbTipoFiltro",tipoFiltro);
			request.setAttribute("canciones", c);
			RequestDispatcher rd = request
					.getRequestDispatcher("/movil/_layout.jsp");
			rd.forward(request, response);
			/*String json = new Gson().toJson(c);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(json);*/
		}
		catch(DAOExcepcion ex){
			//response.getWriter().write('Hubo un error interno');
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.toString());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			System.out.println("Entre a Buscar Servlet");
			String valorABuscar = request.getParameter("valorABuscar")!=null?request.getParameter("valorABuscar"):"";
			String tipoFiltro = request.getParameter("tipoFiltro")!=null?request.getParameter("tipoFiltro"):"";

			GestionCancion negocio = new GestionCancion();
			Collection<Cancion> c = new ArrayList<>();

			switch(tipoFiltro){
			case "1":
				c = negocio.obtenerCancionPorNombre(valorABuscar.trim());
				break;
			case "2":
				c=negocio.obtenerCancionPorArtista(valorABuscar.trim());
				break;
			default:
				break;
			}
			/*request.setAttribute("txtSearch", valorABuscar);
			request.setAttribute("cbTipoFiltro",tipoFiltro);
			request.setAttribute("canciones", c);
			RequestDispatcher rd = request
					.getRequestDispatcher("/movil/_layout.jsp");
			rd.forward(request, response);*/
			String json = new Gson().toJson(c);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(json);
		}
		catch(DAOExcepcion ex){
			//response.getWriter().write('Hubo un error interno');
			System.out.println(ex.toString());
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.toString());
		}

	}

}
